package com.parkreport.report.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.xml.rpc.ServiceException;

import com.common.jdbc.DB4ObjUtil;
import com.common.report.service.ReportService;
import com.common.report.vo.ReportDataVO;
import com.common.report.vo.ReportTitleVO;
import com.parkreport.vo.CityAreaVO;
import com.parkreport.vo.TicketVO;

/**
 * 购票分布统计
 * @author 王剑
 *
 */
public class TicketMarketReportService extends ReportService {

	
	private String parkName;
	private String parkId;
	public String getParkId() {
		return parkId;
	}


	public void setParkId(String parkId) {
		this.parkId = parkId;
	}


	public String getParkName() {
		return parkName;
	}


	public void setParkName(String parkName) {
		this.parkName = parkName;
	}


	@Override
	public List<ReportDataVO> findDatas(HttpServletRequest req)
			throws ServiceException {
		
		List<ReportDataVO> list = new ArrayList<ReportDataVO>();
		String parkId = req.getParameter("parkId");
		String startDate =year+ "-01-01";
		String endDate = year+ "-12-31";
		
		List<TicketVO> ticketList =  findTicket(parkId,startDate,endDate);
		Map<String,Long> ticketMap = new HashMap<String,Long>();
		
		
		Map<String,String> caMap = getCaMap();
		 for (int i = 0; i < ticketList.size(); i++) {
			 TicketVO vo = ticketList.get(i);
			 String city = vo.getCityId();
			 
			 String park = vo.getParkId();
			 String type = vo.getType();
			 String date = vo.getDate();
			 
			long count = vo.getCount();
			 if(caMap.get(city)!=null){
				 String area= caMap.get(city);
				 String key = area+"-"+park+"-"+type+"-"+date;
				 
				 if(ticketMap.get(key)!=null){
					long c =  ticketMap.get(key);
					long ncount = c+count;
					ticketMap.put(key, ncount);
				 }else{
					 ticketMap.put(key, count);
				 }
			 }
			 
		}
		 //组织ReportDataVO================
		 
		 
		
		return list;
	}

	private Map<String,String> getCaMap(){
		String sql = "select * from LAREA_CITY";
		List<CityAreaVO> cityAreaList = DB4ObjUtil.execute4Obj(new CityAreaVO(), sql);
		Map<String,String> caMap = new HashMap<String, String>();
		for (int i = 0; i < cityAreaList.size(); i++) {
			CityAreaVO cavo = cityAreaList.get(i);
			caMap.put(cavo.getCityId(), cavo.getLareaId());
			
		}
		return caMap;
	}
	
	@Override
	public List<ReportTitleVO> findMents(HttpServletRequest req)
			throws ServiceException {
		
		
		List<ReportTitleVO> titleList = new ArrayList<ReportTitleVO>();
		for(int i=1;i<=12; i++){
			String month = ""+i;
			ReportTitleVO vo = new ReportTitleVO(year+"年"+month+"月",month);
			titleList.add(vo);
		}
		log.info("titleList=========="+titleList.size());
		return titleList;
	}

	@Override
	public List<ReportTitleVO> findTitles(HttpServletRequest req)
			throws ServiceException {
		
		List<ReportTitleVO> subList = new ArrayList<ReportTitleVO>();
		subList.add(new ReportTitleVO("团队人数","团队人数") );
		subList.add(new ReportTitleVO("散客人数","散客人数") );
		
		StringBuilder sql = new StringBuilder("");
		sql.append("select NAME as code,NAME as name from LAREA");
		List<ReportTitleVO> list = DB4ObjUtil.execute4Obj(new ReportTitleVO(), sql.toString());
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setTitleList(subList);
			
		}
		log.info("list=========="+list.size());
		return list;
	}

	
	
	
	private List<TicketVO> findTicket(String parkId,String startDate,String endDate){
		List<TicketVO> list = new ArrayList<TicketVO>();
		
		//获得散客购票数
		
		
		
		//获得团队购票数
		
		
		
		
		//获得总购票数
		
		
		
		
		return list;
	}
	
	
	
	
}
